<?php
$flag = 0;
$total_money = 0;
if (isset($_POST) && $_POST != NULL) {
    $money_user = Frontend::findUserById($_SESSION["user_id"]);
    if (isset($_SESSION["user_id"]) && $_SESSION["user_id"] != "") {
        foreach ($_SESSION["cart"] as $key => $_cart) {
            $base_decode = base64_decode($_cart);
            $json_decode = json_decode($base_decode, true);
            if (!$json_decode["money"]) {
                $json_decode = $json_decode + array("money" => $_POST["number_money"][$key]);
                $json_decode = $json_decode + array("index_key" => $_POST["index_key"][$key]);
            } else {
                $json_decode["money"] = $_POST["number_money"][$key];
                $json_decode["index_key"] = $_POST["index_key"][$key];
            }
            $_SESSION["cart"][$key] = $json_decode;
            $total_money = $total_money + $_SESSION["cart"][$key]["money"];
            $json_encode = json_encode($json_decode);
            $base_encode = base64_encode($json_encode);
            $_SESSION["cart"][$key] = $base_encode;
        }
        foreach ($money_user as $_money_user) {
            if ($total_money > (int)$_money_user["gold"]) {
                $flag = 1;
                break;
            } else {
                foreach ($_SESSION["cart"] as $key => $_cart) {
                    try {
                        Sys::addUserOdd($_SESSION["cart"][$key], $_SESSION["user_id"]);
                    } catch (Exception $exc) {
                        echo "Lỗi dự đoán kết quả";
                    }
                }
                $_money_user = $_money_user + array("money" => $total_money);
                Sys::updateUserMoney($_money_user, $_SESSION["user_id"]);
                unset($_SESSION["cart"]);
                $flag = 2;
            }
        }
    } else {
        echo "<script>window.location.href = 'http://dudoanthethao.com.vn/login';</script>";
    }
}
?>
<div id="loaddata">
    <form action="" id="verifyForm" class="form-horizontal" method="post">
        <legend>Dự Đoán Kết Quả</legend>
        <div id="message" class="alert alert-error" style="<?php if($flag == 1) { echo "display: block;"; } else { echo "display: none;"; } ?>"><?php if($flag == 1){ echo "Tài khoản bạn không đủ tiền."; } ?></div>
        <table class="table table-bordered table-events">

            <thead>
                <?php if (isset($_SESSION["cart"])) { ?>
                    <tr class="heading">
                        <th>Trận đấu</th>
                        <th>Chọn</th>
                        <th>Kiểu</th>
                        <th style="width: 63px;">Đặt</th>
                        <th style="width: 63px;">Thắng</th>
                        <th style="width: 85px;"></th>
                    </tr>
                <?php } else { ?>
                    <tr class="heading">
                    </tr>
                <?php } ?>
            </thead>
            <tbody>
                <?php
                if (isset($_SESSION["cart"])) {
                    foreach ($_SESSION["cart"] as $keys => $_cart) {
                        $base_decode = base64_decode($_cart);
                        $json_decode = json_decode($base_decode);
                        $match = Frontend::findMatchById((int) $json_decode->match_id);
                        foreach ($match as $_match) {
                            $team1 = Frontend::findTeamById((int) $_match["Team1"]);
                            $team2 = Frontend::findTeamById((int) $_match["Team2"]);
                            $odd_value = Frontend::findOddById((int) $json_decode->odd_id);
                            foreach ($odd_value as $_odd_value) {
                                $value = explode("|", $_odd_value["value"]);
                                foreach ($arr_odd_bets as $_arr_odd_bets) {
                                    foreach ($value as $key => $_value) {
                                        if ($_value == $json_decode->odd_value) {
                                            $chon_kieu = $_odd_value["title"];
                                            $chon_cua = $_arr_odd_bets[$_odd_value["typeID"]][$key];
                                            $odd_id = $_odd_value["_id"];
                                            $index_key = $key;
                                        }
                                    }
                                }
                            }
                            ?>
                            <tr class="alert">
                                <td style="text-align: left;">
                                    <a href="#">
                                        <?php
                                        foreach ($team1 as $_team1)
                                            echo $_team1["name"] . " - ";
                                        foreach ($team2 as $_team2)
                                            echo $_team2["name"];
                                        ?></a>
                                </td>
                                <td><?php echo $chon_cua ?><span class="label label-warning" style="margin-left: 10px"><?php echo $json_decode->odd_value; ?></span></td>
                                <td><?php echo $chon_kieu ?><input type="hidden" name="index_key[<?php echo $keys ?>]" value="<?php echo $index_key; ?>"/></td>
                                <td>
                                    <input id="number_money_<?php echo $keys ?>" name="number_money[<?php echo $keys ?>]" type="text" maxlength="4" value="<?php
                            if ($json_decode->money)
                                echo $json_decode->money;
                            else
                                echo "0";
                                        ?>" style="width: 50px" onkeydown="checknumber('<?php echo $keys ?>')"/>
                                    <!--<a class="btn bt-log" data-toggle="modal" role="button" href="#login" type="submit">Đăng nhập</a>-->
                                </td>
                                <td>
                                    <input id="total_money_<?php echo $keys ?>" readonly="" type="text" value="<?php
                        if ($json_decode->total_money)
                            echo $json_decode->total_money;
                        else
                            echo "0";
                                        ?>" style="width: 65px"/>
                                </td>
                                <td>
                                    <a id="btnupdate" onclick="update_datcuoc('<?php echo $_cart ?>', '<?php echo $keys ?>', '<?php echo $index_key; ?>')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/update.png"/></a>
                                    <a id="btndelete" onclick="delete_datcuoc('<?php echo $keys ?>')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/delete.png"/></a>
                                </td>
                            </tr>
                        <?php } ?>
                    <?php } ?>
                    <tr class="alert">
                        <!--<td colspan="5" style="text-align: right"><b>Tổng điểm thắng : </b><input id="total_money" readonly="" type="text" value="" style="width: 65px"/></td>-->
                        <td colspan="6" style="text-align: right">
                            <button type="submit" class="btn btn-inverse btn-large" id="btndatcuoc" style="padding: 7px 9px; font-size: 15px">Đặt cược</button>
                        </td>
                    </tr>
                <?php } else { ?>
                    <tr class="alert">
                        <td><?php if ($flag == 2) {
                        echo "Đặt thành công.";
                    } else if($flag == 1) {
                        echo "Tài khoản bạn không đủ tiền.";
                    } else { 
                        echo "Chưa có dự đoán nào.";
                    } 
                    ?></td>
                    </tr>
<?php } ?>
            </tbody>

        </table>
    </form>
</div>
<script>
                                            function checknumber($key) {
                                                $('#number_money_' + $key).keydown(isNumbericOnly);
                                            }

                                            function update_datcuoc($values, $key, $index_key) {
                                                $.ajax({
                                                    // The link we are accessing.
                                                    url: "/ajax.php",
                                                    // The type of request.
                                                    type: "get",
                                                    data: "act=update_datcuoc&values=" + $values + "&key=" + $key + "&money=" + $('#number_money_' + $key).val() + "&index_key=" + $index_key,
                                                    // The type of data that is getting returned.
                                                    dataType: "html",
                                                    error: function() {
                                                    },
                                                    success: function(strData) {
                                                        // Load the content in to the page.
                                                        if (strData != "") {
                                                            $("#message").html("<label>Thay đổi tỷ lệ đặt cược thành công.</label>");
                                                            $('#message').css({display: "block"});
                                                            $('#total_money_' + $key).val(strData);
                                                        }
                                                    }
                                                });
                                            }

                                            function update_all_datcuoc($values, $key) {
                                                $.ajax({
                                                    // The link we are accessing.
                                                    url: "/ajax.php",
                                                    // The type of request.
                                                    type: "get",
                                                    data: "act=update_all_datcuoc&values=" + $values + "&key=" + $key + "&money=" + $('#number_money_' + $key).val(),
                                                    // The type of data that is getting returned.
                                                    dataType: "html",
                                                    error: function() {
                                                    },
                                                    success: function(strData) {
                                                        // Load the content in to the page.
                                                        if (strData == "true") {
                                                            alert('Kèo của trận này đã được đặt');
                                                        } else {
                                                            $("#message").html("<label>" + strData + "</label>");
                                                            $('#message').css({display: "block"});
                                                        }
                                                    }
                                                });
                                            }

                                            function delete_datcuoc($key) {
                                                $.ajax({
                                                    // The link we are accessing.
                                                    url: "/ajax.php",
                                                    // The type of request.
                                                    type: "get",
                                                    data: "act=delete_datcuoc&key=" + $key,
                                                    // The type of data that is getting returned.
                                                    dataType: "html",
                                                    error: function() {
                                                    },
                                                    success: function(strData) {
                                                        // Load the content in to the page.
                                                        if (strData == "true") {
                                                            alert('Kèo của trận này đã được đặt');
                                                        } else {
                                                            $("#loaddata").html(strData);
                                                        }
                                                    }
                                                });
                                            }
</script>